<?php

namespace app\api\controller\v1;

use app\admin\model\BdDatacenter;
use app\admin\model\BdOrg;
use app\admin\model\Customer;
use app\admin\model\Material;
use app\admin\model\Materialimage;
use app\admin\model\Porder;
use app\admin\model\Surface;
use app\erp\controller\QueryArdetail;
use think\Env;

class Querydetail extends Base
{
    public function getQuerydetail()
    {
        $status          = $this->params['fk_bdl_orderstatus'] ?? 1; // 1全部 2 已签收 3 退货


        //日期
        if($this->params['type'] == 5)
        {
            $check      = $this->plulicValidate($this->params,
                ['start_time|开始日期', 'end_time|结束日期'],
                ['require',  'require']);

            if ($check['code'] == 1)
            {
                $this->error($check['msg']);
            }
            $start_time = $this->params['start_time'] . ' 00:00:00';
            $end_time   = $this->params['end_time'] . ' 23:59:59';
        }else{
            $time       = $this->getStartEnd($this->params['type']);
            $start_time = $time['start'];
            $end_time   = $time['end'];
        }

        //分页
        $page               = $this->params['page'] ?? 1;
        $pageSize           = 5;


        $datacenter         = BdDatacenter::where('fnumber',$this->datacenter)->find();
        $query              = new QueryArdetail($start_time,$end_time, "bdl_WeChat", $datacenter['fk_bdl_clientsecret'], $this->datacenter, Env::get('erp.user_name'));
        $custo              = Customer::get($this->customer_id);
        $forg               = BdOrg::get($custo['forgid']);
        $result             = $query->run($custo['fnumber'],$forg['fnumber']);
        if(in_array($result['code'],[0]))
        {
            return $this->error($result['mes']);
        }
        if(in_array($result['code'],[2]))
        {
            return $this->error($result['mes']);
        }
        $data               = $result['data'];
        if(empty($data[0])){
           return  $this->error('此周期对账单没有数据');
        }
        $data               = $data[0];
        $bdl_billno         = $data->bdl_billno;

        //获取角标
        $show_subscript     = [
            'all'           => Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->where('fk_bdl_bd_customer',$this->customer_id)->count(),
            'sign'          => Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_orderstatus',4)->count(),
            'return'        => Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_orderstatus',5)->count(),
        ];

        //全部
        switch ($status)
        {
            case 1:
                $order_mes          = Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->order('fk_bdl_erpbillno','desc')->column('fk_bdl_erpbillno');
                $order_info         = array_chunk($order_mes,$pageSize);
                break;
            case 2:
                $order_mes          = Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->where('fk_bdl_orderstatus',4)->order('fk_bdl_erpbillno','desc')->column('fk_bdl_erpbillno');
                $order_info         = array_chunk($order_mes,$pageSize);

                break;
            case 3:
                $order_mes          = Porder::where('fk_bdl_erpbillno','in',$bdl_billno)->where('fk_bdl_orderstatus',5)->order('fk_bdl_erpbillno','desc')->column('fk_bdl_erpbillno');
                $order_info         = array_chunk($order_mes,$pageSize);
                break;
        }

        if(!array_key_exists($page-1, $order_info))
        {
            $new_data = [];
        }else {


            $order_mes = Porder::where('fk_bdl_erpbillno', 'in', $order_info[$page - 1])->order('fk_bdl_erpbillno', 'desc')->select();


            //前端返回数据
            $new_data = [];

            foreach ($order_mes as $k => $v) {
                $new = [
                    'fk_bdl_erpbillno' => $v['fk_bdl_erpbillno'],
                    'fk_bdl_allqty' => $v['fk_bdl_allqty'],
                    'fk_bdl_allamount' => $v['fk_bdl_allamount'],
                    'fk_bdl_ordertime' => $v['fk_bdl_ordertime'],
                    'fk_bdl_orderstatus' => $v['fk_bdl_orderstatus'],
                    'fk_bdl_ordertype' => 1,
                    'look_time' => $v['fk_bdl_ordertime'],


                    'fk_bdl_planshiptime' => $v['fk_bdl_planshiptime'],
                    'fk_bdl_signtime' => $v['fk_bdl_signtime'],
                    'fk_bdl_planreceivetime' => $v['fk_bdl_planreceivetime'],
                    'id' => $v['id']
                ];

                $son = [];
                $dejson_data = json_decode($v['fk_bdl_orderjsondata'], true);

                if (!empty($dejson_data)) {
                    foreach ($dejson_data as $kk => $vv) {
                        $img = Material::where('fname', $vv['bdl_bd_material_name'])
                            ->whereIn('forgid', $this->forgids)
                            ->where('fk_bdl_bd_datacenter', $this->datacenter)
                            ->find();

                        //表面
                        $surface_id = Surface::where('fnumber', $vv['bdl_bd_surface_number'])
                            ->where('fk_bdl_bd_datacenter', $this->datacenter)
                            ->whereIn('forgid', $this->forgids)->find();

                        $material_id = $img['id'];
                        if (!empty($surface_id) && !empty($material_id)) {
                            $find = Materialimage::where('fk_bdl_bd_surface', $surface_id['id'])
                                ->where('fk_bdl_bd_material', $material_id)->find();
                            if (!empty($find)) {
                                $show_image = $find['fk_bdl_imageurl'];
                            } else {
                                $show_image = $this->getDefaultImage();
                            }
                        } else {
                            $show_image = $this->getDefaultImage();
                        }

                        $son[] = [
                            'texture' => !empty($vv['bdl_bd_texture_name']) ? $vv['bdl_bd_texture_name'] : '',
                            'surface' => !empty($vv['bdl_bd_surface_name']) ? $vv['bdl_bd_surface_name'] : '',
                            'bdl_thickness' => !empty($vv['bdl_thickness']) ? floatval($vv['bdl_thickness']) : '',
                            'bdl_width' => !empty($vv['bdl_width']) ? floatval($vv['bdl_width']) : '',
                            'bdl_refthickness' => !empty($vv['bdl_refthickness']) ? floatval($vv['bdl_refthickness']) : '',
                            'bdl_length' => !empty($vv['bdl_length']) ? floatval($vv['bdl_length']) : '',
                            'bdl_unitid_name' => !empty($vv['bdl_unitid_name']) ? $vv['bdl_unitid_name'] : '',
                            'bdl_amount' => !empty($vv['bdl_salamt']) ? handle_money($vv['bdl_salamt']) : '',
                            'bdl_qty' => !empty($vv['bdl_qty']) ? floatval($vv['bdl_qty']) : '',
                            'bdl_materialgrade' => !empty($vv['bdl_bd_material_grade']) ? $vv['bdl_bd_material_grade'] : '',
                            'url' => $show_image
                        ];
                    }
                }
                $new['son'] = $son;

                $new_data[] = $new;
            }
        }


    //        bdl_initbalance_bal  期末欠款
    //        bdl_initbalance_in   应付
    //        bdl_initbalance_out  实付
    //        bdl_initbalance      期初现款
        $response_data      = [
            'bdl_initbalance_bal'       => $data->bdl_initbalance_bal,
            'bdl_initbalance_in'        => $data->bdl_initbalance_in,
            'bdl_initbalance_out'       => $data->bdl_initbalance_out,
            'bdl_initbalance'           => $data->bdl_initbalance,
            'data'                      => $new_data,
            'show_sub'                  => $show_subscript
        ];


        $this->success('查询成功',$response_data);

    }


}